package com.frejoys.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.StoreActivityUpDto;
import com.frejoys.common.dto.admin.StoreIdAndNameDto;
import com.frejoys.common.dto.admin.UpShelvesDto;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.admin.AdminStoreActivityInfoVo;
import com.frejoys.common.vo.admin.AdminStoreActivityPageVo;
import com.frejoys.common.vo.store.StoreActivityVo;
import com.frejoys.dao.entity.StoreActivity;
import com.frejoys.service.StoreActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "店铺活动", description = "店铺活动管理")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixAdmin)
@Authorization(Authority.Private)
public class AdminStoreActivityController {

    private final StoreActivityService storeActivityService;

    private final RedissonClient redissonClient;

    @Operation(summary = "活动详情")
    @PostMapping("/storeActivity/detail")
    public Result<StoreActivityVo> offShelve(@RequestBody @Validated IdDto dto) {
        StoreActivity activity = storeActivityService.getById(dto.getId());
        return Result.success(BeanUtil.copyProperties(activity, StoreActivityVo.class));
    }

    /**
     * 根据店铺活动id获取店铺活动详情
     */
    @Operation(summary = "根据店铺活动id获取店铺活动详情")
    @PostMapping("/storeActivity/info")
    public Result<AdminStoreActivityInfoVo> getInfo(@RequestBody @Validated IdDto dto){
        return Result.success(storeActivityService.getInfo(dto));
    }

    /**
     * 后台查询店铺活动
     */
    @Operation(summary = "活动列表")
    @PostMapping("/storeActivity/pageList")
    public Result<IPage<AdminStoreActivityPageVo>> pageList(@RequestBody @Validated StoreIdAndNameDto dto) {
        return Result.success(storeActivityService.pageList(dto));
    }


    @Operation(summary = "编辑店铺活动")
    @PostMapping("/storeActivity/upStoreActivity")
    public Result upStoreActivity(@RequestBody @Validated StoreActivityUpDto dto) {
        RLock lock = redissonClient.getLock(CacheKeyUtil.upStoreActivityLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                storeActivityService.upStoreActivity(dto);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }


    @Operation(summary = "编辑店铺活动上下架状态")
    @PostMapping("/storeActivity/upStoreActivityShelves")
    public Result upStoreActivityShelves(@RequestBody @Validated UpShelvesDto dto) {
        RLock lock = redissonClient.getLock(CacheKeyUtil.upStoreActivityLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                storeActivityService.upStoreActivityShelves(dto);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }
}
